starter.controller('bookingLinesController', function($rootScope,$state,$ionicPopup,$timeout,$scope,Page, localStore,sessionStore,$location,$ionicHistory) {
	
		$scope.customerLines = {
			catalogs : [],
			all : [],
			searchKey : '',
			query : function(){
				if(this.all.length > 0){
					return;
				}
				Page.ajaxPost('line/customer/opened.do',null,function(resp){
					$scope.customerLines.all = resp.data;
					$scope.customerLines.catalogs = resp.data;
					$scope.$watch('customerLines.searchKey', function (newValue, oldValue) {
						$scope.customerLines.catalogs = [];
						if(typeof newValue != 'undefined' && newValue != ''){
							$($scope.customerLines.all).each(function(idx,catalog){
								 if(catalog.catalog != '' && catalog.catalog.indexOf(newValue) != -1){
									 $scope.customerLines.catalogs.push(catalog);
									 return;
								 }
								 var lines = catalog.lines;
								 $(lines).each(function(idx,line){
									 if(line.lineName != '' && line.lineName.indexOf(newValue) != -1){
										 $scope.customerLines.catalogs.push(catalog);
										 return false;
									 }
								 });
							});
						}else{
							$scope.customerLines.catalogs = $scope.customerLines.all;
						}
				    });
				});
			},
			booking : function(line){
				Page.ajaxPostQuiet('line/stops.do',{lineId : line.lineId},function(resp){
					line.stops = resp.data;
					sessionStore.setObject('booking.line', line);
					sessionStore.setObject('booking.order', null);
					$state.go('booking_plan', {line: line}, {reload:true});
				});
			}
		};
		
		$scope.newLines = {
				catalogs : [],
				all : [],
				searchKey : '',
				query : function(){
					if(this.all.length > 0){
						return;
					}
					Page.ajaxPost('line/customer/toBeOpen.do',null,function(resp){
						$scope.newLines.all = resp.data;
						$scope.newLines.catalogs = resp.data;
						$scope.$watch('newLines.searchKey', function (newValue, oldValue) {
							$scope.newLines.catalogs = [];
							if(typeof newValue != 'undefined' && newValue != ''){
								$($scope.newLines.all).each(function(idx,catalog){
									 if(catalog.catalog != '' && catalog.catalog.indexOf(newValue) != -1){
										 $scope.newLines.catalogs.push(catalog);
										 return;
									 }
									 var lines = catalog.lines;
									 $(lines).each(function(idx,line){
										 if(line.lineName != '' && line.lineName.indexOf(newValue) != -1){
											 $scope.newLines.catalogs.push(catalog);
											 return false;
										 }
									 });
								});
							}else{
								$scope.newLines.catalogs = $scope.newLines.all;
							}
					    });
					});
				},
				booking : function(line){
					Page.ajaxPostQuiet('line/stops.do',{lineId : line.lineId},function(resp){
						line.stops = resp.data;
						$state.go('booking_plan', {line: line}, {reload:true});
					});
				}
			};
		
		$scope.staffLines = {
			catalogs : [],
			all : [],
			searchKey : '',
			query : function(){
				if(this.all.length > 0){
					return;
				}
				Page.ajaxPost('line/employee/opened.do',null,function(resp){
					$scope.staffLines.all = resp.data;
					$scope.staffLines.catalogs = resp.data;
					$scope.$watch('staffLines.searchKey', function (newValue, oldValue) {
						$scope.staffLines.catalogs = [];
						if(typeof newValue != 'undefined' && newValue != ''){
							$($scope.staffLines.all).each(function(idx,catalog){
								 if(catalog.catalog != '' && catalog.catalog.indexOf(newValue) != -1){
									 $scope.staffLines.catalogs.push(catalog);
									 return;
								 }
								 var lines = catalog.lines;
								 $(lines).each(function(idx,line){
									 if(line.lineName != '' && line.lineName.indexOf(newValue) != -1){
										 $scope.staffLines.catalogs.push(catalog);
										 return false;
									 }
								 });
							});
						}else{
							$scope.staffLines.catalogs = $scope.staffLines.all;
						}
				    });
				});
			},
			booking : function(line){
				Page.ajaxPostQuiet('line/stops.do',{lineId : line.lineId},function(resp){
					line.stops = resp.data;
					$state.go('booking_plan', {line: line}, {reload:true});
				});
			}
		};
		
		
});